using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;



namespace Mogocal.util
{
    public class Util
    {
        public static void SaveConfig(ConfigurationSettings conf)
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(ConfigurationSettings));
                TextWriter writer = new StreamWriter(getSettingsFileName());
                serializer.Serialize(writer, conf);
                writer.Close();
            }
            catch (IOException)
            {
                ShowMessageError("An error occcurred during saving the configuration file.");
            }
            catch (XmlException)
            {
                ShowMessageError("An error occcurred during saving the configuration file.");
            }
        }

        /// <summary>
        /// Show an Error Message
        /// </summary>
        /// <param name="msgError"></param>
        public static void ShowMessageError(string msgError)
        {
            MessageBox.Show(msgError, "MoGoCal Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }

        /// <summary>
        /// Get settings filename
        /// </summary>
        /// <returns></returns>
        public static string getSettingsFileName()
        {
            StringBuilder s = new StringBuilder();

            s.Append(".");
            s.Append(Path.DirectorySeparatorChar);
            s.Append("conf");
            s.Append(Path.DirectorySeparatorChar);
            s.Append("MogoCalSettings.xml");

            return s.ToString();
        }
    }
}
